//
//  MyLoginSetViewController.m
//  OneWordNew
//
//  Created by qianfeng001 on 15-7-7.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "MyLoginSetViewController.h"
#import "ChangeNameViewController.h"
#import "ChangePSWViewController.h"
#import "SDImageCache.h"
#import "ChangeTXViewController.h"
#import "MySetViewController.h"
@interface MyLoginSetViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
{
    UIImagePickerController *_imagePicker;
    NSString *_filePath;
    UIActionSheet *_myActionSheet;

}
@end

@implementation MyLoginSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    NSString *name=[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
    self.namelable.text=name;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showAliertWithTitle:(NSString *)title message:(NSString *)message{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];

}
- (IBAction)changeimageClick:(UIButton *)sender {
   // [self creatUI];
}
- (void)creatUI{
    _myActionSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开照相机",@"从手机相册获取", nil];
    _myActionSheet.delegate=self;
    //  [_myActionSheet shouInView:self.view];
    [_myActionSheet showInView:self.view];
    
    // [self.view addSubview:_myActionSheet];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == _myActionSheet.cancelButtonIndex)
    {
        NSLog(@"取消");
    }
    
    switch (buttonIndex)
    {
        case 0:  //打开照相机拍照
            [self takePhoto];
            break;
            
        case 1:  //打开本地相册
            [self LocalPhoto];
            break;
    }
    
}
- (void)takePhoto{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        
        [self presentModalViewController:picker animated:YES];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
    
}
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentModalViewController:picker animated:YES];
    
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
        
        //得到选择后沙盒中图片的完整路径
        _filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
        
        //关闭相册界面
        [picker dismissModalViewControllerAnimated:YES];
        
        //创建一个选择后图片的小图标放在下方
        //类似微薄选择图后的效果
        UIImageView *smallimage = [[UIImageView alloc] initWithFrame:
                                   CGRectMake(0, 0, 40, 40)];
        
        smallimage.image = image;
        //加在视图中
        [self.view addSubview:smallimage];
        
    }
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissModalViewControllerAnimated:YES];
}


- (IBAction)changeNameClick:(UIButton *)sender {
    ChangeNameViewController *change=[[ChangeNameViewController alloc] init];
    [self.navigationController pushViewController:change animated:YES];
    
    
}

- (IBAction)changgePawd:(UIButton *)sender {
    ChangePSWViewController *change=[[ChangePSWViewController alloc] init];
    [self.navigationController pushViewController:change animated:YES];
}

- (IBAction)clearRubbshClick:(UIButton *)sender {
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"总共有%.2fM缓存",[self getCachSize]] preferredStyle:UIAlertControllerStyleActionSheet];
    //增加按钮
    [sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //删除
        //删除两部分
        //1.删除 sd 图片缓存
        //先清除内存中的图片缓存
        [[SDImageCache sharedImageCache] clearMemory] ;
        //清除磁盘的缓存
        [[SDImageCache sharedImageCache] clearDisk];
        
        //2.删除自己缓存
        NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
        [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
        
        
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //取消
    }]];
    //跳转
    [self presentViewController:sheet animated:YES completion:nil];
    
    
    
}

- (IBAction)getOutClick:(UIButton *)sender {
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    NSString *name=[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
    token=nil;
    name=nil;

    NSLog(@"%@",token);
    NSLog(@"%@",name);
    NSUserDefaults *ud= [NSUserDefaults standardUserDefaults];
    [ud setObject:nil forKey:@"token"];
    [ud setObject:nil forKey:@"nickname"];
    [ud setObject:@"1" forKey:@"bool"];
    [ud synchronize];
    NSLog(@"%@",token);
    NSLog(@"%@",name);
    MySetViewController *set=[[MySetViewController alloc] init];
    //[self.view removeFromSuperview];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"getOut" object:nil];
    [self.navigationController pushViewController:set animated:YES];
    
    return;
    
}
- (CGFloat)getCachSize {
    //爱限免 有两类 缓存  sdWebImage 和 首页
    //获取图片缓存大小
    NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
    //获取自定义缓存大小
    //用枚举器遍历 一个文件夹的内容
    //1.获取 文件夹枚举器
    //
    
    NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
    __block NSUInteger count = 0;
    //2.遍历
    for (NSString *fileName in enumerator) {
        NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
        count += fileDict.fileSize;//自定义所有缓存大小
    }
    // 得到是字节  转化为M
    CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
    return totalSize;
}

- (void)viewWillAppear:(BOOL)animated{
    NSString *name=[[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
    
    self.namelable.text=name;

}
@end
